App.instance = function() {
	this.init();
};

Ext.apply(App.instance.prototype, {

	init : function() {
		var northPanel = this.getNorthPanel();
		var westPanel = this.getWestPanel();
		var centerPanel = this.getCenterPanel();
		this.viewport = new Ext.Viewport({
			layout : 'border',
			items : [ northPanel, westPanel, centerPanel ]
		});
	},

	getNorthPanel : function() {
		var panel;
		if (this.viewport) {
			panel = this.viewport.items.get(0);
		} else {
			panel = new Ext.Panel({
				region : 'north',
				contentEl : 'north',
				autoHeight : true,
				border : false,
				split : true,
				margins : '0 0 0 0'
			});
		}
		return panel;
	},

	getWestPanel : function() {
		var panel;
		if (this.viewport) {
			panel = this.viewport.items.get(1);
		} else {
			panel = new Ext.tree.TreePanel({
				region : 'west',
				collapsible : true,
				width : 200,
				margins : '0 0 5 5',
				tools : [ {
					id : 'pin'
				}, {
					id : 'unpin'
				} ],
				collapseFirst : false,
				autoScroll : true,
				split : true,
				title : '我的控制台',
				collapseMode : 'mini',
				loader : new Ext.tree.TreeLoader(),
				root : new Ext.tree.AsyncTreeNode({
					text : '我是根',
					expanded : true,
					children : [ {
						text : '工具',
						expanded : true,
						children : [ {
							id : '1',
							text : '模块配置',
							url : '/flower/web/system/T_SYS_MOD/index.jsp',
							leaf : true
						}, {
							id : '2',
							text : '邮件箱',
							url : '/flower/web/module/mailbox/default.jsp',
							// icon : contextPath +
							// '/page/home/images/user.png',
							iconCls : 'aa',
							leaf : true
						}, {
							id : '3',
							text : '日历',
							url : '/flower/web/module/calendar/index.jsp',
							// icon : contextPath +
							// '/page/home/images/user.png',
							iconCls : 'aa',
							leaf : true
						}, {
							id : '4',
							text : '建模工具',
							url : '/component/model/model.js',
							iconCls: 'icon_user',
							leaf : true
						}, {
							id : '5',
							text : '告警',
							url : '/page/alarm/index.jsp',
							leaf : true
						} ]
					} ]
				}),
				rootVisible : false
			});

			panel.on('click', this.onTreeClick, this);
		}
		return panel;
	},

	getCenterPanel : function() {
		var panel;
		if (this.viewport) {
			panel = this.viewport.items.get(2);
		} else {
			panel = new Ext.TabPanel({
				region : 'center',
				margins : '0 5 5 0',
				activeTab : 0,
				plain : true,
				tabWidth : 150,
				defaults : {
					border : false
				},
				items : [ {
					title : '首页',
					iconCls : 'icon_home',
					autoLoad: contextPath + '/home/home.jsp'
				} ]
			});
		}
		return panel;
	},

	onTreeClick : function(node) {
		var center = this.getCenterPanel();
		var tabId = 'tab_' + node.id;
		var url = node.attributes.url;
		var iconCls = node.attributes.iconCls;
		
		if (!url) {
			return;
		}
		var tab = center.get(tabId);
		if (!tab) {
			Ext.Ajax.request({
				url: contextPath + url,
				success: function(response, opts){
					var T = eval(response.responseText);
					var p = new T({
						id: tabId,
						title: node.text,
						closable: true,
						iconCls: node.attributes.iconCls
					});
					tab = center.add(p);
					center.activate(tab);
				}
			});
		} else {
			center.activate(tab);
		}
	}

});